package com.example.daily.data.dao

import androidx.room.*
import com.example.daily.data.entity.FoodRecord
import kotlinx.coroutines.flow.Flow

@Dao
interface FoodRecordDao {
    @Insert
    suspend fun insertFoodRecord(foodRecord: FoodRecord)

    @Query("SELECT * FROM food_records WHERE userId = :userId AND recordDate BETWEEN :startDate AND :endDate ORDER BY recordDate DESC")
    fun getFoodRecordsBetweenDates(userId: Int, startDate: Long, endDate: Long): Flow<List<FoodRecord>>

    @Query("SELECT SUM(calories) FROM food_records WHERE userId = :userId AND recordDate BETWEEN :startDate AND :endDate")
    fun getTotalCaloriesBetweenDates(userId: Int, startDate: Long, endDate: Long): Flow<Int?>

    @Query("SELECT * FROM food_records WHERE userId = :userId AND recordDate >= :startOfDay AND recordDate < :endOfDay ORDER BY recordDate DESC")
    fun getFoodRecordsForDay(userId: Int, startOfDay: Long, endOfDay: Long): Flow<List<FoodRecord>>

    @Delete
    suspend fun deleteFoodRecord(foodRecord: FoodRecord)

    @Query("SELECT * FROM food_records WHERE userId = :userId AND recordDate BETWEEN :startTime AND :endTime ORDER BY recordDate DESC")
    suspend fun getFoodRecordsByDateRange(userId: Int, startTime: Long, endTime: Long): List<FoodRecord>

    @Query("SELECT SUM(calories) FROM food_records WHERE userId = :userId AND recordDate BETWEEN :startTime AND :endTime")
    suspend fun getTotalCaloriesByDateRange(userId: Int, startTime: Long, endTime: Long): Int?

    @Query("SELECT * FROM food_records WHERE userId = :userId AND recordDate BETWEEN :startTime AND :endTime ORDER BY recordDate DESC")
    fun getFoodRecordsForUserInTimeRange(userId: Int, startTime: Long, endTime: Long): Flow<List<FoodRecord>>
} 